package com.zsc.common.web.handler;

import com.alibaba.fastjson.TypeReference;
import com.zsc.common.web.result.BaseResult;
import com.zsc.common.web.util.CodeEnum;
import com.zsc.common.web.util.HttpClientHolder;
import com.zsc.common.web.util.HttpClientUtil;
import com.zsc.common.web.util.JsonUtils;
import com.zsc.common.web.util.*;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;

/**
 * AbstractUdsServiceHandler.java是互动平台的用户认证服务交互接口处理器抽象类。
 *
 * @author jason
 * @version 1.0 2017年3月16日
 */
public abstract class AbstractUdsServiceHandler extends ServiceHandlerAdapter {

    /**
     * 日志器
     */
    private static final Logger logger = Logger.getLogger(AbstractUdsServiceHandler.class);

    /**
     * 用户认证中心服务系统服务器URL
     */
    @Value(value = "${commons.uds.server.url}")
    protected String serverUrl;

    /**
     * 是否成功标识
     */
    private boolean isSuccess;

    /**
     * 响应数据
     */
    private String responseData;

    /*
     * (non-Javadoc)
     *
     * @see com.qucai8.common.handler.ServiceHandler#name()
     */
    @Override
    public String name() {
        return "uds";
    }

    /*
     * (non-Javadoc)
     *
     * @see com.qucai8.common.handler.ServiceHandler#service()
     */
    @Override
    public void service() {
        CloseableHttpClient httpClient = null;
        try {
            httpClient = HttpClientHolder.getHttpClient();
            String requestContent = getRequestData();
            if (requestContent == null) {
                requestContent = "{\"version\" : \"1.0\"}";
            }
            HttpEntity requestEntity = new StringEntity(requestContent, ContentType.APPLICATION_JSON);
            String requestUrl = serverUrl + getInterfaceName();
            CloseableHttpResponse response = HttpClientUtil.post(httpClient, requestUrl, requestEntity, null);
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                responseData = EntityUtils.toString(responseEntity, Constant.DEFAULT_CHARSET_NAME);
                BaseResult result = JsonUtils.fromJson(responseData, new TypeReference<BaseResult>() {});
                if (result.getCode() == CodeEnum.code8200.getCode()) {
                    isSuccess = true;
                    logger.debug("【UDS】向用户认证中心请求操作成功");
                } else {
                    isSuccess = false;
                    logger.error("【UDS】向用户认证中心请求操作失败");
                }
            }
        } catch (Exception e) {
            logger.error("【UDS】向用户认证中心请求操作时发生异常", e);
        }
    }

    /**
     * 获取接口名称。
     *
     * @return 接口名称
     */
    public abstract String getInterfaceName();

    /**
     * 获取请求数据。
     *
     * @return 请求数据
     */
    public abstract String getRequestData();

    /**
     * @return the responseData
     */
    public String getResponseData() {
        return responseData;
    }

    /**
     * @return the isSuccess
     */
    public boolean isSuccess() {
        return isSuccess;
    }

    /**
     * @param serverUrl the serverUrl to set
     */
    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }
}
